package org.example.DAOImp;

import org.example.DAO.StudentDAO;
import org.example.DBUtil;
import org.example.Student;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class StudentDAOImp implements StudentDAO {
    @Override
    public void add(Student student) {
        try{
            DBUtil.getSessionFactory().inSession(session -> {
                Transaction ts=session.beginTransaction();
                session.persist(student);
                ts.commit();
                session.close();
            });
        }catch (Exception e){
            System.out.println("dd");
        }

    }

    @Override
    public void update(Student student) {
        Session session= DBUtil.getSessionFactory().openSession();
        Transaction ts=session.beginTransaction();
        session.merge(student);
        ts.commit();
        session.close();
    }

    @Override
    public void delete(Student student) {
        Session session= DBUtil.getSessionFactory().openSession();
        Transaction ts=session.beginTransaction();
        session.remove(student);
        ts.commit();
        session.close();
    }

    @Override
    public Student getStudentByID(String studentId) {
        Session session= DBUtil.getSessionFactory().openSession();
        Student s= session.get(Student.class,studentId);
        session.close();
        return s;
    }
}
